const point = require('../models/point')
const homework = require('../models/homework')
const question = require('../models/question')
const workRecords = require('../models/workRecords')

var express = require('express')
var router = express.Router()

var formidable = require('formidable')

router.post('/', (req, res) => {
    var form = new formidable.IncomingForm()
    form.parse(req, function (err, fields, files) {
        if(err) {
            res.json({'state': false, 'msg': err})
        }else {
            homework.findOne({
                seq: fields.seq
            }, async (err, work) => {
                if(err) {
                    res.json({'state': false, 'msg': err})
                }else {
                    var find_point = (point_id) => {
                        return new Promise(function (resolve, reject) {
                            point.findOne({
                                _id: point_id
                            }, (err, doc) => {
                                if(err) {
                                    res.json({'state': false, 'msg': err})
                                }else {
                                    resolve(doc);
                                }
                            })
                        })
                    }
            
                    var find_question = (point_id, question_id) => {
                        return new Promise(function (resolve, reject) {
                            point.findOne({
                                _id: point_id
                            }, (err, p) => {
                                if(err) {
                                    res.json({'state': false, 'msg': err})
                                }else{
                                    question.findOne({
                                        _id: question_id
                                    }, (err, doc) => {
                                        if(err) {
                                            res.json({'state': false, 'msg': err})
                                        }else {
                                            resolve({
                                                point: p,
                                                question: doc
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    }
                    let last_time = work.time;
                    last_time = new Date(last_time)
                    let now_time = new Date();
                    let is_late = true;
                    if(now_time < last_time) {
                        is_late = false
                    }
                    // now_time = formatDate(new Date(now_time+(8*60*60*1000)))
                    let question_answer = [];
                    let illustration_answer = [];
                    if(work.type === 'timu') {
                        for(var i=0;i<fields.question_answer.length;i++){
                            let tmpquestion_answer = fields.question_answer[i];
                            let tmppointid = tmpquestion_answer.pointId;
                            let tmpquestionid = tmpquestion_answer.questionId;
                            let tmpstu_answer = tmpquestion_answer.stu_answer;
                            let tmp_result = true;
                            let tmp_ill_answer = {
                                pointId: tmppointid,
                                questionId: tmpquestionid
                            }
                            if(tmpquestionid){
                                let findques = await find_question(tmppointid, tmpquestionid).then();
                                if(findques.point && findques.question){
                                    if(findques.question.type !== 'jianda' && (
                                        tmpstu_answer.sort().toString() !== findques.question.answer.sort().toString()
                                    )) {
                                        tmp_result = false;
                                    }
                                    tmp_ill_answer.answer = findques.question.answer;
                                    tmp_ill_answer.illustrationContent = findques.question.illustrationContent;
                                }
                            }else {
                                let findques = await find_point(tmppointid).then();
                                if(findques){
                                    if(findques.practice.type === 'xuanze' && (
                                        tmpstu_answer.sort().toString() !== findques.practice.answer.sort().toString()
                                    )) {
                                        tmp_result = false;
                                    }else if(findques.practice.type === 'panduan' && (
                                        tmpstu_answer[0] !== findques.practice.answer
                                    )){
                                        tmp_result = false;
                                    }
                                    tmp_ill_answer.answer = findques.practice.answer;
                                    tmp_ill_answer.illustrationContent = findques.practice.illustrationContent;
                                }
                            }
                            tmpquestion_answer.result = tmp_result;
                            question_answer.push(tmpquestion_answer);
                            illustration_answer.push(tmp_ill_answer);
                        }
                    }else if(work.type === 'kaifang') {
                        let tmp = fields.question_answer[0];
                        tmp.result = true;
                        question_answer.push(tmp);
                        
                    }
                    workRecords.findOne({
                        seq: fields.seq,
                        UUID: fields.UUID
                    }, (err, record) => {
                        if(err) {
                            res.json({'state': false, 'msg': err})
                        }else {
                            if(!record){
                                workRecords.create({
                                    flags: fields.flag,
                                    UUID: fields.UUID,
                                    seq: fields.seq,
                                    type: work.type,
                                    question_answer: question_answer,
                                    time: now_time,
                                    is_late: is_late
                                }, (err, docs) => {
                                    if(err){
                                        res.json({'state': false, 'msg': err});
                                    }else {
                                        work.submit_num += 1;
                                        work.save((err, doc)=>{
                                            if(err){
                                                res.json({'state':false, 'msg': err})
                                            }else {
                                                res.json({
                                                    'state':true,
                                                    'msg': '提交作业成功！',
                                                    'is_late': is_late,
                                                    'question_answer': question_answer,
                                                    'illustration_answer': illustration_answer
                                                })
                                            }
                                        })
                                    }
                                });
                            }else {
                                record.flags = fields.flag;
                                record.question_answer = question_answer;
                                record.time = now_time;
                                record.is_late = is_late;
                                record.save((err, doc) => {
                                    if(err) {
                                        res.json({'state': false, 'msg': err})
                                    }else {
                                        res.json({
                                            'state': true,
                                            'msg': '作业修改成功！',
                                            'is_late': is_late,
                                            'question_answer': question_answer,
                                            'illustration_answer': illustration_answer
                                        })
                                    }
                                })
                            }
                        }
                    })
                }
            });
        }
    });
});

module.exports = router;